package com.duomizhibo.phonelive.fragment;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.duomizhibo.phonelive.AppContext;
import com.duomizhibo.phonelive.R;
import com.duomizhibo.phonelive.api.remote.PhoneLiveApi;
import com.duomizhibo.phonelive.bean.UserBean;
import com.duomizhibo.phonelive.event.Event;
import com.duomizhibo.phonelive.model.YanZhengBean;
import com.duomizhibo.phonelive.ui.TCVideoRecordActivity;
import com.duomizhibo.phonelive.ui.dialog.CommonToast;
import com.duomizhibo.phonelive.utils.UIHelper;
import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;


/**
 * Created by carolsuo on 2017/3/7.
 * 短视频或者直播选择界面
 */

public class PublisherDialogFragment extends DialogFragment {

    View mTVLive;
    View mTVVideo;
    ImageView mIVClose;
    protected UserBean mUser;
    Context context;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        context=getContext();


        final Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.fragment_publisher);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        mUser = AppContext.getInstance().getLoginUser();
        mTVLive = dialog.findViewById(R.id.tv_live);

        mTVVideo = dialog.findViewById(R.id.tv_video);

        mIVClose = (ImageView) dialog.findViewById(R.id.publisher_close);

        mTVLive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                startLive();
            }
        });

        mTVVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                startActivity(new Intent(PublisherDialogFragment.this.getActivity(), TCVideoRecordActivity.class));
//                http://apizb.meibo520.com/api/public/?service=User.setCash&uid=11499&token=280fce736c0588e343c85d183e962ba3

           /*     PhoneLiveApi.yanzheng(mUser.id, mUser.token, new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.d(call.toString(),"eeeeeeeeeeeee");
                    }

                    @Override
                    public void onResponse(String response, int id) {
Log.d(response.toString(),"33333333333");
                        Gson gson=new Gson();
                        YanZhengBean yanZhengBean=new YanZhengBean();
                        yanZhengBean=gson.fromJson(response.toString(),YanZhengBean.class);
                        if (yanZhengBean.getData().getCode()==0){
//                            startActivity(new Intent(PublisherDialogFragment.getActivity(), TCVideoRecordActivity.class));
//                            startActivity(new Intent(PublisherDialogFragment.this.getActivity(), TCVideoRecordActivity.class));
//                            context.startActivity(new Intent(getActivity(),TCVideoRecordActivity.class));
//                            Toast.makeText(context, yanZhengBean.getData().getMsg()+"", Toast.LENGTH_SHORT).show();
//                            requestStartLive();

                        EventBus.getDefault().post(1);
                        }else {
                            Toast.makeText(context, yanZhengBean.getData().getMsg()+"", Toast.LENGTH_SHORT).show();
                        }


                        Log.d(response.toString(),"eeeeeeeeeeeee");

                    }
                });*/





            }
        });


        mIVClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });


        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        return dialog;
    }

    //开始直播初始化
    public void startLive() {
        if (Build.VERSION.SDK_INT >= 23) {
            //摄像头权限检测
            if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO)
                            != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED
                    ||
                    ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED) {
                //进行权限请求
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.ACCESS_COARSE_LOCATION},
                        5);

            } else {
                requestStartLive();
            }
        } else {
            requestStartLive();
        }

    }

    //请求服务端开始直播
    private void requestStartLive() {
        UIHelper.showStartLiveActivity(getActivity());
    }


    public void onEventMainThread(Integer type) {
        Log.e("---", "EventBus收到int:" + type);

if (type==1){
    startActivity(new Intent(PublisherDialogFragment.this.getActivity(), TCVideoRecordActivity.class));
}

    }

}
